package com.share.finance.risk.core.listener;

import com.share.finance.risk.core.service.CreditLoanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class CreditLoanListener {
    @Autowired
    private CreditLoanService creditLoanService;

    @KafkaListener(topics = {"credit", "pre_credit", "loan", "pre_loan"}, groupId = "test")
    public void creditLoanApply(List<ConsumerRecord<String, String>> records, Acknowledgment ack) {
        //1.业务处理
        try {
            for (ConsumerRecord<String, String> record : records) {
                creditLoanService.creditLoanApply(record.value());
            }
        } catch (Exception e) {
            log.error("kafka消费异常", e);
        }

        //2.提交偏移量
        ack.acknowledge();
    }
}
